package com.source.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.source.sys.entity.Popedom;
import com.source.sys.entity.Role;
import com.source.sys.entity.RolePopedom;
import com.source.sys.mapper.RoleMapper;
import com.source.sys.mapper.RolePopedomMapper;
import com.source.sys.service.PopedomService;
import com.source.sys.service.RolePopedomService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.source.sys.service.RoleService;
import com.source.sys.vo.PopedomVo;
import com.source.sys.vo.RolePopedomVo;
import com.source.sys.vo.RoleVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ZZF
 * @since 2021-02-08
 */
@Service
public class RolePopedomServiceImpl extends ServiceImpl<RolePopedomMapper, RolePopedom> implements RolePopedomService {
    @Resource
    private RolePopedomMapper rolePopedomMapper;
    @Resource
    private PopedomService popedomService;
    @Resource
    private RoleService roleService;
    @Resource
    private RoleMapper roleMapper;

    @Override
    public boolean addRolePopedom(String roleName,String popedomId) {
        //添加角色
        roleService.addRole(new Role(roleName,null,null,1,null));
        //查询角色id
        Role role = roleService.getOne(new QueryWrapper<Role>().eq("rname", roleName));
        //添加角色权限(RolePopedom(String rid,String popedomId))
        int insertRolePopedom = rolePopedomMapper.insert(new RolePopedom(role.getRid(), popedomId));
        return insertRolePopedom>0?true:false;
    }

    @Override
    public boolean addRolePopedom(String roleName, List<String> popedomIdList) {
        //添加角色
        roleService.addRole(new Role(roleName,null,null,1,null));
        //查询角色id
        Role role = roleService.getOne(new QueryWrapper<Role>().eq("rname", roleName));
        //添加角色权限(RolePopedom(String rid,String popedomId))
        int insertRolePopedom = 0;
        if(!popedomIdList.isEmpty()){
            for (String pid : popedomIdList) {
                insertRolePopedom = rolePopedomMapper.insert(new RolePopedom(role.getRid(), pid));
                if(insertRolePopedom <= 0){
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    public Page<RoleVo> listRolePage(int currentPageNo, int size) {
//        Page<Role> rolePage = roleService.listRolePage(currentPageNo, size);
        return roleMapper.listRoleVoPage(new Page<>(currentPageNo, size));
    }

    @Override
    public List<Popedom> listPopedomByRid(String roleId) {
        List<Popedom> popedomList = new ArrayList<>();
        List<RolePopedom> rolePopedomList = rolePopedomMapper.selectList(new QueryWrapper<RolePopedom>().eq("roleId", roleId));
        for (RolePopedom rolePopedom : rolePopedomList) {
            String popedomId = rolePopedom.getPopedomId();
            Popedom popedom = popedomService.getPopedomById(popedomId);
            popedomList.add(popedom);
        }
        return popedomList;
    }

    @Override
    public List<PopedomVo> listPopedomNameByRid(String roleId) {
        List<PopedomVo> popedomNameList = new ArrayList<>();
        List<RolePopedom> rolePopedomList = rolePopedomMapper.selectList(new QueryWrapper<RolePopedom>().eq("roleId", roleId));
        //获取RolePopedom列表
        for (RolePopedom rolePopedom : rolePopedomList) {
            //获取权限id，根据权限id查询权限对象，放入List集合
            String popedomId = rolePopedom.getPopedomId();
            Popedom popedom = popedomService.getPopedomById(popedomId);
            popedomNameList.add(new PopedomVo(popedom.getPid(),popedom.getPName()));
        }
        return popedomNameList;
    }

    @Override
    public Set<Popedom> listLevel2PopedomByRid(String roleId) {
        List<String> popedomNameList = new ArrayList<>();
        //获取RolePopedom列表
        List<RolePopedom> rolePopedomList = rolePopedomMapper.selectList(new QueryWrapper<RolePopedom>().eq("roleId", roleId));
        Set<Popedom> popedomSet = new HashSet<>();
        for (RolePopedom rolepopedom : rolePopedomList) {
            String popedomId = rolepopedom.getPopedomId();
            //获取上级菜单
            Popedom popedomLevel2 = popedomService.getPrePopedomById(popedomId);
            popedomSet.add(popedomLevel2);
        }
        //获取
        return popedomSet;
    }

    @Override
    public RolePopedomVo showRolePopedomInfoByRoleId(String roleId) {
//        Map<String, Object> rolePopedomMap = new HashMap<>(1);

        Role role = roleService.showRoleById(roleId);
        if(role == null){
            return null;
        }
        String roleName = role.getRName();
        List<Popedom> popedomList = listPopedomByRid(roleId);
        RolePopedomVo rolePopedomVo = new RolePopedomVo(roleId,roleName,popedomList);
        return rolePopedomVo;
    }

    @Override
    public boolean updateRolePopedomByRoleId(String roleId, String roleName, List<String> popedomIdList) {
        //修改角色名
        boolean updateRole = roleService.updateRoleNameByRid(roleId, roleName);
        if(updateRole == true){
            //修改角色名成功
            //修改权限
            //先删再加
            int deleteResult = rolePopedomMapper.delete(new QueryWrapper<RolePopedom>().eq("roleId", roleId));
            int insertResult = 0;
            for (String popedomId : popedomIdList) {
                insertResult = rolePopedomMapper.insert(new RolePopedom(roleId, popedomId));
            }
            return true;
        }else{
            //修改角色名失败
            return false;
        }


    }
}
